package Dao;

import bean.OutpatientAppointment;
import bean.check_record;

import java.util.List;

public class outpatientDao extends BaseDao{
    public List<OutpatientAppointment> queryRecords() throws Exception {
        String sql = "SELECT * FROM outpatient_record";
        return queryForList(OutpatientAppointment.class,sql);
    }

    public int addRecord(OutpatientAppointment record) {
        String sql = "INSERT INTO outpatient_record(patient_name,patient_gender,patient_age,IdNumber,doctor_name,appointment_status,created_time,Sign_in_situation,Emergency,appointment_date,appointment_time) VALUES(?,?,?,?,?,?,?,?,?,?,?)";
        try {
            return update(sql,record.getPatient_name(),record.getPatient_gender(),record.getPatient_age(),record.getIdNumber(),record.getDoctor_name(),record.getAppointment_status(),record.getCreated_time(),record.getSign_in_situation(),record.getEmergency(),record.getAppointment_date(),record.getAppointment_time());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }


    public int deleteRecordBYId(Integer id) {
        String sql = "DELETE FROM outpatient_record WHERE Outpatient_id= ?";
        try {
            return update(sql,id);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public OutpatientAppointment queryRecordBYId(Integer id) {
        String sql = "SELECT Outpatient_id ,patient_name,patient_gender,patient_age,IdNumber,doctor_name,appointment_status,created_time,Sign_in_situation,Emergency,appointment_date,appointment_time FROM outpatient_record WHERE Outpatient_id = ?";
        try {
            return queryForOne(OutpatientAppointment.class,sql,id);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public List<OutpatientAppointment> queryRecordBYStatus(String status) {
        String sql = "SELECT Outpatient_id ,patient_name,patient_gender,patient_age,IdNumber,doctor_name,appointment_status,created_time,Sign_in_situation,Emergency,appointment_date,appointment_time FROM outpatient_record WHERE appointment_status = ?";
        try {
            return queryForList(OutpatientAppointment.class,sql,status);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    public OutpatientAppointment queryRecordBYName(String name) {
        String sql = "SELECT Outpatient_id ,patient_name,patient_gender,patient_age,IdNumber,doctor_name,appointment_status,created_time,Sign_in_situation,Emergency,appointment_date,appointment_time FROM outpatient_record WHERE patient_name = ?";
        try {
            return queryForOne(OutpatientAppointment.class,sql,name);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    public List<OutpatientAppointment> queryRecordBYStatusAndSign(String status,String sign_status) {
        String sql = "SELECT Outpatient_id ,patient_name,patient_gender,patient_age,IdNumber,doctor_name,appointment_status,created_time,Sign_in_situation,Emergency,appointment_date,appointment_time FROM outpatient_record WHERE appointment_status = ? AND Sign_in_situation = ? ";
        try {
            return queryForList(OutpatientAppointment.class,sql,status,sign_status);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    public List<OutpatientAppointment> queryRecordBYSign(String sign_status) {
        String sql = "SELECT Outpatient_id ,patient_name,patient_gender,patient_age,IdNumber,doctor_name,appointment_status,created_time,Sign_in_situation,Emergency,appointment_date,appointment_time FROM outpatient_record WHERE  Sign_in_situation = ? ";
        try {
            return queryForList(OutpatientAppointment.class,sql,sign_status);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    //获取病人签到状态
    public String querySignStatus(String name,String created_time) throws Exception {
        String sql = "SELECT Sign_in_situation FROM outpatient_record WHERE patient_name = ? AND created_time = ? ";
        return (String) queryForSingleValue(sql,name,created_time);
    }

    //更新病人预约状态
    public int update_appointment_Status(OutpatientAppointment record,String status) throws Exception {
        String sql = "update outpatient_record set appointment_status = ?  where Outpatient_id = ? ";
        return update(sql,status,record.getOutpatient_id());
    }

    //更新病人签到状态
    public int update_sign_Status(OutpatientAppointment record,String status) throws Exception {
        String sql = "update outpatient_record set Sign_in_situation = ?  where Outpatient_id = ? ";
        return update(sql,status,record.getOutpatient_id());
    }

}
